﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Xna.Framework;

namespace UnityEditor.Controls.ComponentControl.Editors
{
    /// <summary>
    /// Interaction logic for Vector2Editor.xaml
    /// </summary>
    public partial class Vector2Editor : UserControl
    {
        public Vector2Editor()
        {
            InitializeComponent();
        }

        public static readonly DependencyProperty Vector2ValueProperty = DependencyProperty.Register("Vector2Value", typeof(Vector2), typeof(Vector2Editor),
                                new FrameworkPropertyMetadata(null));
        public Vector2 Vector2Value
        {
            get
            {
                return (Vector2)this.GetValue(Vector2ValueProperty);
            }
            set
            {
                this.SetValue(Vector2ValueProperty, value);
            }
        }
    }

    public class Vector2Converter : IValueConverter
    {
        private Vector2 _originalValue; // the static struct that stores original value at the start of editing

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            _originalValue = ((Vector2)value);

            if (parameter.ToString() == "X")
                return ((Vector2)value).X;
            if (parameter.ToString() == "Y")
                return ((Vector2)value).Y;

            return _originalValue;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            float parsedValue = 0.0f;
            try
            {
                parsedValue = float.Parse(value.ToString());
            }
            catch (Exception)
            {
                MessageBox.Show("Input value should be float.", "Caution", MessageBoxButton.OK);
            }
            finally
            {
                if (parameter.ToString() == "X")
                    _originalValue = new Vector2(parsedValue, _originalValue.Y);
                if (parameter.ToString() == "Y")
                    _originalValue = new Vector2(_originalValue.X, parsedValue);
            }

            return _originalValue;
        }
    }
}
